<?php
/**
 * 菜品管理
 * @author hushuai
 *
 */
class DishesAction extends CommonAction {
 
	private $tableDishes     = "Dishes";     //定义菜品表
	private $tableRdrelation = "Rdrelation"; //定义餐厅菜品关联表
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 查询菜品分类（分页）
     */
    public function index(){
    	 
    	$where    = $this->_get("where",          "trim",   "");
    	$desc     = $this->_get("orderDirection", "trim",   "desc");;
    	$order    = $this->_get("orderName",      "trim",   "addtime");
    	$pageNum  = $this->_get("pageNum",        "intval", C('PAGE_NUMS'));
    	$nowPage  = $this->_get("p",              "intval", 1);
    	$isdel    = $this->_get("isdel",          "intval", 2);
    	$con      = $order.' '.$desc;
    	$table    = $this->tableDishsort;
    	
    	$map = array("isdel"=>$isdel);
    	
    	if($where){            //如果存在则定义查询条件
    		$map['title']       = array("like",'%'.$where.'%');
    		$map['entitle']     = array("like",'%'.$where.'%');
    		$map['dishes_num']  = array("like",'%'.$where.'%');
    		$map['_logic']      = 'or';
    	}
    	
    	$this->success(parent::pageSelect($table,$map,$pageNum,$nowPage,$con));
    	 
    }
    
    /**
     * 添加菜品
     */
    public function add(){
    	
    	parent::do_add($this->tableDishes);
    	
    }
    
    /**
     * 添加餐厅菜品关联信息
     */
    public function addRelation(){
  
    	$param = array(
    			
    			"before" => "rdrelation_add_before",
    			"return" => false
    	);
    	parent::do_add($this->Rdrelation,$param);
    	 
    }
    
    /**
     * 修改菜品分类
     */
    public function edit(){
    	 
    	parent::do_edit($this->tableDishsort);
    	 
    }
    
    /**
     * 删除菜品分类
     */
    public function del(){
    
    	$id = $this->_get("id","intval",0);
    		
    	$res = M($this->tableDishsort)->where("pid=".$id)->find();
    	if($res){
    		$this->error("该分类内还有子类别，不能删除！");
    	}else{
    		parent::do_del($this->tableDishsort,true);   //删除菜品分类表中相关数据
    	}
    
    }
    
    /**
     * 菜品分类有子类
     */
    public function view(){
    	
    	$map = array("isdel"=>2,"issub"=>1);
    	$this->success(M($this->tableDishsort)->where($map)->select());
    	 
    }
    
    /**
     * 菜品分类无分页查询
     */
    public function select(){
    	 
    	$map = array("isdel"=>2);
    	
    	$this->success(M($this->tableDishsort)->where($map)->select());
    
    }
    
    /**
     * 根据id查询
     */
    public function find(){
    
    	$id = $this->_get("id","intval",0);
    	 
    	$this->success(M($this->tableDishsort)->find($id));
    
    }
   
}